﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.AppService;
using Windows.Foundation.Collections;
using Xiaowei.Services;
using Xiaowei.Services.TencentVideoArgs;

namespace Xiaowei.Players
{
    public class VideoPlayer : IPlayer
    {
        ITencentVideoService TencentVideoService => ServiceFactory.TencentVideoService;
        private Queue<KeyValuePair<PlayerCommandType, object>> commandList = new Queue<KeyValuePair<PlayerCommandType, object>>();

        public bool IsInstalled { get; set; } = false;
        public bool IsLaunched { get; set; } = false;

        public PlayStateCombine PlayStateCombine { get; private set; }

        public event EventHandler<PlayStateCombine> PlayerStateChanged;

        public VideoPlayer()
        {

            //ConnectionManager.AddHandler("VideoState", (v) =>
            //{
            //    PlayerState state = (int)v["state"] == 0 ? PlayerState.Pause : PlayerState.Play;
            //    string ID = v["ID"].ToString();
            //
            //    if (PlayStateCombine.ID != ID || PlayStateCombine.PlayerState != state)
            //    {
            //        PlayStateCombine.ID = ID;
            //        PlayStateCombine.PlayerState = state;
            //        PlayerStateChanged?.Invoke(this, PlayStateCombine);
            //    }
            //});
            string msgId = "";
            TencentVideoService.SendMessage(ref msgId, VoiceMessageCode.GET_STATE, "get_state", false);
            PlayStateCombine = new PlayStateCombine() { ID="", PlayerState= PlayerState.Stop,Title="" };
        }

        private void VideoStateChanged(string ID,int State)
        {
            PlayerState state = State == 0 ? PlayerState.Pause : PlayerState.Play;

            if (PlayStateCombine.ID != ID || PlayStateCombine.PlayerState != state)
            {
                PlayStateCombine.ID = ID;
                PlayStateCombine.PlayerState = state;
                PlayerStateChanged?.Invoke(this, PlayStateCombine);
            }
        }

        public void Launch()
        {

        }
        public void Play(object item)
        {
            Logger.Debug.WriteLine("Play video start");
            if(item is string s)
            {
                int result = TencentVideoService.SendVoiceText(item.ToString());
                if (result == -4)
                {
                    IsInstalled = false;
                }
                else
                {
                    IsInstalled = true;
                }
                Logger.Debug.WriteLine("Play video end");
            }
        }

        public void Play()
        {
            if (isSuspend)
            {
                resumePauseState = false;
                return;
            }
            if( PlayStateCombine.PlayerState == PlayerState.Stop)
            {
                return;
            }
            TencentVideoService.SendVoiceText("开始");
        }
        public void Stop()
        {
            if (isSuspend)
            {
                resumePauseState = true;
                return;
            }
            TencentVideoService.SendVoiceText("停止");
        }
        public void Pause()
        {
            if (isSuspend)
            {
                resumePauseState = true;
                return;
            }

            this.handlePause();
        }
        private  void handlePause()
        {
            if(!TencentVideoService.VideoIsLive())
            {
                PlayStateCombine.PlayerState = PlayerState.Stop;
            }
            if (PlayStateCombine.PlayerState != PlayerState.Play) return;
            TencentVideoService.SendVoiceText("暂停");
        }
        public void Prev()
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.Prev, null));
                return;
            }
            TencentVideoService.SendVoiceText("上一集");
        }
        public void Next()
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.Next, null));
                return;
            }
            TencentVideoService.SendVoiceText("下一集");
        }
        public void PlayByRandom()
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.PlayByRandom, null));
                return;
            }
            TencentVideoService.SendVoiceText("随机播放");
        }
        public void PlayByOrder()
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.PlayByOrder, null));
                return;
            }
            TencentVideoService.SendVoiceText("顺序播放");
        }
        public void PlayByListLoop()
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.PlayByListLoop, null));
                return;
            }
            TencentVideoService.SendVoiceText("列表循环");
        }
        public void PlayBySingleLoop()
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.PlayBySingleLoop, null));
                return;
            }
            TencentVideoService.SendVoiceText("单片循环");
        }
        public void FastForward(int seconds)
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.FastForward, seconds));
                return;
            }
            TencentVideoService.SendVoiceText("快进" + seconds + "秒");
        }
        public void FastRewind(int seconds)
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.FastRewind, seconds));
                return;
            }
            TencentVideoService.SendVoiceText("快退" + seconds + "秒");
        }
        public void SeekTo(int seconds)
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.SeekTo, seconds));
                return;
            }
            TencentVideoService.SendVoiceText("跳转到" + seconds + "秒");
        }
        bool resumePauseState = false;
        bool isSuspend = true;
        public void Suspend()
        {
            if (!isSuspend)
            {
                commandList.Clear();
                resumePauseState = PlayStateCombine.PlayerState != PlayerState.Play;
                Pause();
                isSuspend = true;
            }
        }
        public void Resumed()
        {
            if (!isSuspend) return;
            isSuspend = false;
            while (commandList.Count > 0)
            {
                var command = commandList.Dequeue();
                switch (command.Key)
                {
                    case PlayerCommandType.Launch:
                        Launch();
                        break;
                    case PlayerCommandType.Play:
                        Play((string)command.Value);
                        break;
                    case PlayerCommandType.Prev:
                        Prev();
                        break;
                    case PlayerCommandType.Next:
                        Next();
                        break;
                    case PlayerCommandType.FastForward:
                        FastForward((int)command.Value);
                        break;
                    case PlayerCommandType.FastRewind:
                        FastRewind((int)command.Value);
                        break;
                    case PlayerCommandType.SeekTo:
                        SeekTo((int)command.Value);
                        break;
                    case PlayerCommandType.PlayByOrder:
                        PlayByOrder();
                        break;
                    case PlayerCommandType.PlayByRandom:
                        PlayByRandom();
                        break;
                    case PlayerCommandType.PlayByListLoop:
                        PlayByListLoop();
                        break;
                    case PlayerCommandType.PlayBySingleLoop:
                        PlayBySingleLoop();
                        break;
                    default:
                        break;
                }
            }
            if (!resumePauseState)
            {
                Play();
            }
        }
        public void Resumed2()
        {
            if (!isSuspend) return;
            isSuspend = false;
            commandList.Clear();
            if (!resumePauseState)
            {
                Play();
            }
        }
    }
}
